package st.lib.base.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import st.lib.base.log.bean.LogException;
import st.lib.base.log.bean.LogMethod;
import st.lib.base.util.utilcode.util.AppUtils;
import st.lib.base.util.utilcode.util.NetworkUtils;

/**
 * @author zhangyu
 * @date 2018/9/20
 * 方法的调用日志记录
 */

public class LogAction implements IBehaviorAction {
    //方法调用日志的上传阀值
    private static final int METHOD_LOG_THRESHOLD_VALUE = 5;
    //异常日志的上传阀值
    private static final int EXCEPTION_LOG_THRESHOLD_VALUE = 1;
    private static List<LogMethod> sLogMethods = new ArrayList<>();
    private List<LogException> mLogExceptions;
    private SimpleDateFormat mSimpleDateFormat =
            new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss:SSS", Locale.CHINA);
    private Date mDate;
    private LogMethod mLogMethod;

    @Override
    public void doWithPointBefore(ProceedingJoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Object target = joinPoint.getTarget();
        Method method;
        try {
            Object[] args = joinPoint.getArgs();
            method = target.getClass().getMethod(methodSignature.getName(),
                    methodSignature.getParameterTypes());
            //方法名
            String name = method.getName();
            //类名
            String className = target.getClass().getSimpleName();
            mDate = new Date(System.currentTimeMillis());
            //当前时间
            String currentTime = mSimpleDateFormat.format(mDate);
            //应用名称
            String appName = AppUtils.getAppName();
            //版本号
            String versionCode = AppUtils.getAppVersionName();
            //系统版本
            String systemVersion = AppUtils.getSystemVersion();
            //获取当前网络状态
            NetworkUtils.NetworkType networkType = NetworkUtils.getNetworkType();
            mLogMethod = new LogMethod();
            mLogMethod.setAppName(appName);
            mLogMethod.setClassName(className);
            mLogMethod.setCurrentTime(currentTime);
            mLogMethod.setMethodName(name);
            mLogMethod.setNetState(networkType.toString());
            mLogMethod.setSystemVersion(systemVersion);
            mLogMethod.setVersionCode(versionCode);
           // mLogMethod.setUserAccount(AccountChecker..getAccount());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void doWithPointAfter(ProceedingJoinPoint joinPoint) {
        if (mLogMethod != null) {
            mLogMethod.setExeTime(new Date(System.currentTimeMillis()).getTime() - mDate.getTime());
            if (sLogMethods != null) {
                sLogMethods.add(mLogMethod);
            }
        }
    }
}
